package com.msb.desginpatterns.singleton;

/**
 * 懒汉式单例模式
 */
public class Mgr03 {

    /**
     * 私有无参构造方法
     */
    private Mgr03(){}

    /**
     * 构造一个实例
     */
    private static Mgr03 INSTANCE;

    /**
     * 创建方法，返回实例
     */
    private static Mgr03 getInstance(){
        if(INSTANCE == null){
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            INSTANCE = new Mgr03();
        }
        return INSTANCE;
    }
    /**
     * 测试方法
     */
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                System.out.println(Mgr03.getInstance().hashCode());
            }).start();
        }
    }
}
